import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zchope_student/apis.dart';
import 'package:zchope_student/beans/eventbean.dart';
import 'package:zchope_student/beans/schoolListBean.dart';
import 'package:zchope_student/const.dart';
import 'package:zchope_student/routes.dart';
import 'package:zchope_student/utils/mydio.dart';
import 'package:zchope_student/utils/utils.dart';

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage>
    with AutomaticKeepAliveClientMixin {
  var banners = List();
  List<SchoolListBean> items = List();
  String barcode = "";
  String pxcxCode = "";
  String bh = "";
  String addressCode = '';
  var _mySubscription;
  SchoolListBean schoolListBean;
  var areaCode;
  SharedPreferences sp;
  var loadingDialog;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _mySubscription = eventBus.on<EventBean>().listen((data) {
      print('data:${data.title}');
      if (data.title == Const.EVENT_LOCATION) {
        areaCode = data.content;
        _getSp();
      }
    });

    _getSp();
  }

  _getSp() async {
    sp = await SharedPreferences.getInstance();
    bh = sp.getString(Const.UUID);
    if (Utils.getInstance().checkEmpty(sp.get(Const.AREA_CODE))) {
    } else {
      areaCode = sp.get(Const.AREA_CODE);
    }

    _getListForSchool(areaCode);
    setState(() {});
  }

  _getListForSchool(String areaCode) {
    print('areacode:$areaCode');
//    if (areaCode == null || areaCode == '') {130100
    areaCode = areaCode.substring(0, 4) + '00';
    print('areaCode!!!!!$areaCode');
//    }

    MyDio.getInstance().clearInstance();
    MyDio.getInstance(url: Const.TESTURL).get('${Apis.companyFind}$areaCode',
        (data) {
      print('data!!!!!$data');
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }
      for (var s in data['data']) {
        items.add(SchoolListBean.fromJson(s));
      }
      setState(() {
        print(' :${items.length}');
      });
    });
  }

  _buildList() {
    List<Widget> list = List();
    for (SchoolListBean bean in items) {
      list.add(GestureDetector(
        onTap: () {},
        child: buildListItem(bean),
      ));
    }
    return list;
  }

  Widget buildListItem(SchoolListBean bean) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: 12,
        ),
        Stack(
          alignment: Alignment.topLeft,
          children: <Widget>[
            Stack(
              alignment: Alignment.bottomCenter,
              children: <Widget>[
                Container(
                  height: 260,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(8.0)),
                    image: DecorationImage(
                      image: AssetImage(
                        'images/items.png',
                      ),
                      centerSlice: Rect.fromLTRB(270.0, 180.0, 1360.0, 730.0),
                    ),
                  ),
                ),
                Opacity(
                  opacity: 0.5,
                  child: Container(
                    height: 73,
                    color: Colors.black,
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(left: 13, right: 13, bottom: 15),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          Text(
                            bean.name,
                            style: TextStyle(fontSize: 14, color: Colors.white),
                          ),
                          SizedBox(
                            height: 7,
                          ),
                          Text(
                            '地址：' + bean.address,
                            style: TextStyle(fontSize: 13, color: Colors.white),
                          )
                        ],
                      ),
                      Text(
                        bean.busiscope == null ? '666666' : bean.busiscope,
                        style: TextStyle(fontSize: 14, color: Const.gold),
                      )
                    ],
                  ),
                )
              ],
            ),
            Padding(
              padding: EdgeInsets.only(top: 17, left: 13),
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.black45,
                  borderRadius: BorderRadius.all(Radius.circular(8.0)),
                ),
                child: Padding(
                  padding: EdgeInsets.fromLTRB(7, 4, 7, 4),
                  child: Text(
                    '<900m',
                    style: TextStyle(fontSize: 12, color: Colors.white),
                  ),
                ),
              ),
            )
          ],
        )
      ],
    );
  }

  Future<Null> _onRefresh() async {
    _getSp();
    return;
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
        onRefresh: _onRefresh,
        child: Padding(
          padding: EdgeInsets.only(left: 14, right: 14),
          child: ListView(
            children: <Widget>[
              SizedBox(
                height: 230,
                child: Swiper(
                  itemBuilder: (BuildContext context, int index) {
                    return Image.asset(
                      "images/items2.png",
                      fit: BoxFit.fill,
                    );
                  },
                  itemCount: 1,
                  pagination: SwiperPagination(
                      builder: DotSwiperPaginationBuilder(
                    size: 7,
                    activeSize: 7,
                    color: Colors.grey,
                    activeColor: Colors.white,
                  )),
                  autoplay: true,
                  scrollDirection: Axis.horizontal,
//                control: SwiperControl(),
                ),
              ),
              SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: Stack(
                      alignment: Alignment.center,
                      children: <Widget>[
                        Container(
                          height: 80,
                          child: Image.asset(
                            'images/jiaxiao.png',
                            fit: BoxFit.fill,
                          ),
                        ),
                        Opacity(
                          opacity: 0.4,
                          child: Container(
                            height: 80,
                            decoration: BoxDecoration(
                              color: Colors.black45,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8.0)),
                            ),
                          ),
                        ),
                        Text(
                          '驾校',
                          style: TextStyle(fontSize: 15, color: Colors.white),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  Expanded(
                    flex: 1,
                    child: Stack(
                      alignment: Alignment.center,
                      children: <Widget>[
                        Container(
                          height: 80,
                          child: Image.asset('images/jiaoxue.png'),
                        ),
                        Opacity(
                          opacity: 0.4,
                          child: Container(
                            height: 80,
                            decoration: BoxDecoration(
                              color: Colors.black45,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(8.0)),
                            ),
                          ),
                        ),
                        Text(
                          '教学',
                          style: TextStyle(fontSize: 15, color: Colors.white),
                        )
                      ],
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  Expanded(
                    flex: 1,
                    child: GestureDetector(
                      onTap: () {
                        String route = '${Routes.lessonsOnline}';
                        Routes.router.navigateTo(context, route,
                            replace: false, transition: TransitionType.fadeIn);
                      },
                      child: Stack(
                        alignment: Alignment.center,
                        children: <Widget>[
                          Container(
                            height: 80,
                            child: Image.asset('images/baoming.png'),
                          ),
                          Opacity(
                            opacity: 0.4,
                            child: Container(
                              height: 80,
                              decoration: BoxDecoration(
                                color: Colors.black45,
                                borderRadius:
                                    BorderRadius.all(Radius.circular(8.0)),
                              ),
                            ),
                          ),
                          Text(
                            '远程教学',
                            style: TextStyle(fontSize: 15, color: Colors.white),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 8,
                  ),
                  MyButton(
                    image: 'images/xueche.png',
                    text: '学车',
                    onTap: () {
                      //判断是否绑定身份证
                      _mySubscription =
                          eventBus.on<EventBean>().listen((EventBean data) {
                        var code = Utils.getInstance().checkLogin();
                        code.then((onValue) {
                          if (onValue == Const.UNSIGN) {
                            String route = '${Routes.loginMain}';
                            Routes.router.navigateTo(context, route,
                                replace: false,
                                transition: TransitionType.fadeIn);
                          } else if (onValue == Const.UNBIND) {
                            String route = '${Routes.userBind}';
                            Routes.router.navigateTo(context, route,
                                replace: false,
                                transition: TransitionType.fadeIn);
                          } else {
                            String route = '${Routes.lessons}';
                            Routes.router.navigateTo(context, route,
                                replace: false,
                                transition: TransitionType.fadeIn);
                          }
                        });
                      });

                      var code = Utils.getInstance().checkLogin();
                      code.then((onValue) {
                        if (onValue == Const.UNSIGN) {
                          String route = '${Routes.loginMain}';
                          Routes.router.navigateTo(context, route,
                              replace: false,
                              transition: TransitionType.fadeIn);
                        } else if (onValue == Const.UNBIND) {
                          String route = '${Routes.userBind}';
                          Routes.router.navigateTo(context, route,
                              replace: false,
                              transition: TransitionType.fadeIn);
                        } else {
                          String route = '${Routes.lessons}';
                          Routes.router.navigateTo(context, route,
                              replace: false,
                              transition: TransitionType.fadeIn);
                        }
                      });
                    },
                  ),
//                  Expanded(
//                    flex: 1,
//                    child: GestureDetector(
//                      onTap: () {
//                        //判断是否绑定身份证
//                        _mySubscription =
//                            eventBus.on<EventBean>().listen((EventBean data) {
//                              var code = Utils.getInstance().checkLogin();
//                              code.then((onValue) {
//                                if (onValue == Const.UNSIGN) {
//                                  String route = '${Routes.loginMain}';
//                                  Routes.router.navigateTo(context, route,
//                                      replace: false,
//                                      transition: TransitionType.fadeIn);
//                                } else if (onValue == Const.UNBIND) {
//                                  String route = '${Routes.userBind}';
//                                  Routes.router.navigateTo(context, route,
//                                      replace: false,
//                                      transition: TransitionType.fadeIn);
//                                } else {
//                                  String route = '${Routes.lessons}';
//                                  Routes.router.navigateTo(context, route,
//                                      replace: false,
//                                      transition: TransitionType.fadeIn);
//                                }
//                              });
//                            });
//
//                        var code = Utils.getInstance().checkLogin();
//                        code.then((onValue) {
//                          if (onValue == Const.UNSIGN) {
//                            String route = '${Routes.loginMain}';
//                            Routes.router.navigateTo(context, route,
//                                replace: false,
//                                transition: TransitionType.fadeIn);
//                          } else if (onValue == Const.UNBIND) {
//                            String route = '${Routes.userBind}';
//                            Routes.router.navigateTo(context, route,
//                                replace: false,
//                                transition: TransitionType.fadeIn);
//                          } else {
//                            String route = '${Routes.lessons}';
//                            Routes.router.navigateTo(context, route,
//                                replace: false,
//                                transition: TransitionType.fadeIn);
//                          }
//                        });
//                      },
//                      child: Stack(
//                        alignment: Alignment.center,
//                        children: <Widget>[
//                          Container(
//                            height: 80,
//                            child: Image.asset('images/xueche.png'),
//                          ),
//                          Opacity(
//                            opacity: 0.4,
//                            child: Container(
//                              height: 80,
//                              decoration: BoxDecoration(
//                                color: Colors.black45,
//                                borderRadius:
//                                BorderRadius.all(Radius.circular(8.0)),
//                              ),
//                            ),
//                          ),
//                          Text(
//                            '学车',
//                            style: TextStyle(fontSize: 15, color: Colors.white),
//                          )
//                        ],
//                      ),
//                    ),
//                  ),
                ],
              ),
              items.length == 0
                  ? Container()
                  : Column(
                      children: _buildList(),
                    )
            ],
          ),
        ));
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

class MyButton extends StatelessWidget {
  final image;
  final onTap;
  final text;

  const MyButton({Key key, this.image, this.onTap, this.text})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      flex: 1,
      child: GestureDetector(
        onTap: onTap,
        child: Stack(
          alignment: Alignment.center,
          children: <Widget>[
            Container(
              height: 80,
              child: Image.asset(image),
            ),
            Opacity(
              opacity: 0.4,
              child: Container(
                height: 80,
                decoration: BoxDecoration(
                  color: Colors.black45,
                  borderRadius: BorderRadius.all(Radius.circular(8.0)),
                ),
              ),
            ),
            Text(
              text,
              style: TextStyle(fontSize: 15, color: Colors.white),
            )
          ],
        ),
      ),
    );
  }
}
